package juc_15_future;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

public class Demo01 {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        /*CompletableFuture<Void> completableFuture = CompletableFuture.runAsync(()->{
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + " runAsync=>Void");
        });
        System.out.println("main");
        completableFuture.get();*/

        Integer result = CompletableFuture.supplyAsync(() -> {
            try {
                TimeUnit.SECONDS.sleep(2);
                // int i = 10/0;
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + " runAsync=>Integer");
            return 1024;
        }).whenComplete((t, u) -> {
            System.out.println(t);
            System.out.println(u);
        }).exceptionally(e -> {
            e.printStackTrace();
            System.out.println(e.getMessage());
            return 2333;
        }).get();
        System.out.println("main");
        System.out.println(result);
    }
}
